package srx.open.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import srx.open.com.R;
import srx.open.com.mode.AddrItem;
import srx.open.com.utils.Log;
import srx.open.com.widgets.ScrollForeverTextView;

/**
 * 城市选择界面.
 */
public class AddrActivity extends ExtendBaseActivity {

    private static final String TAG = "AddrActivity";

    public static final int RESULT_ADDR_STATE = 2015;

    // 跳转view的状态.
    private static final int JMP_FIRST_NUM = 0;
    private static final int JMP_SECOND_NUM = 1;
    private static final int JMP_THREE_NUM = 2;

    // 标题栏
    EditText search_et;
    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.left_lay)
    LinearLayout leftLay;
    @Bind(R.id.left_tv)
    ScrollForeverTextView leftTv;
    @Bind(R.id.right_tv)
    TextView rightTv;
    @Bind(R.id.right_lay)
    LinearLayout rightLay;

    @Bind(R.id.search_et)
    EditText searchEt;
    @Bind(R.id.select_addr_tv)
    TextView selectAddrTv;
    @Bind(R.id.province_listview)
    ListView provinceListview;
    @Bind(R.id.first_lay)
    LinearLayout firstLay;
    @Bind(R.id.city_listview)
    ListView cityListview;
    @Bind(R.id.district_listview)
    ListView districtListview;

    //
    private int jmpState = 0;
    private Animation animationIn;
    private Animation animationOut;

    private List<AddrItem> mProvinceList = new ArrayList<AddrItem>();
    private AddrAdapter mAddrProvinceAdapter;

    private List<AddrItem> mCityList = new ArrayList<AddrItem>();
    private AddrAdapter mAddrCityAdapter;

    private List<AddrItem> mDistrictList = new ArrayList<AddrItem>();
    private AddrAdapter mAddrDistrictAdapter;

    private ArrayList<AddrItem> allAddrItemLists = new ArrayList<AddrItem>() {
        {
            add(new AddrItem()); // addr1 比如 四川省
            add(new AddrItem()); // addr2 比如 成都市
            add(new AddrItem()); // addr3 比如 武侯区
        }
    };
    private String saveProvince = "";

    @Override
    public void initAllDatas() {
        mProvinceList = mDataBaseHelper.queryProvince();
    }

    @Override
    public void initAllViews() {
        setContentView(R.layout.activity_addr_detailed);
        ButterKnife.bind(this);
        // 初始化.
        initBundleDatas();
        initTitles();
        initListView();
    }

    private void initBundleDatas() {
        // 获取已经选择的地址.
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String selectAddr = extras.getString("selectAddr");
            selectAddrTv.setText(selectAddr);
        }
    }

    private void initTitles() {
        backIv.setImageResource(R.drawable.addr_left_close);
        rightLay.setVisibility(View.GONE);
        leftTv.setText("添加城市信息");
    }

    private void initListView() {
        // Province
        mAddrProvinceAdapter = new AddrAdapter(mProvinceList);
        provinceListview.setAdapter(mAddrProvinceAdapter);
        provinceListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                hideSoftInputView();
                if (jmpState == JMP_FIRST_NUM) {
                    // 更新数据.
                    AddrItem province = mProvinceList.get(position);
                    allAddrItemLists.set(0, province); // 保存.
                    mCityList = mDataBaseHelper.queryCity(province.getId());
                    if (mCityList.size() > 0) {
                        mCityList.add(0, null);
                        saveProvince = province.getName();
                        leftTv.setText("" + province.getName());
                        mAddrCityAdapter.setDataUpdate(mCityList);
                        setJmpInSecondView();
                    } else {
                        setActivityResult(province);
                    }
                }
            }
        });
        // City.
        mAddrCityAdapter = new AddrAdapter(mCityList);
        cityListview.setAdapter(mAddrCityAdapter);
        cityListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (jmpState == JMP_SECOND_NUM) {
                    // 更新/显示数据.
                    AddrItem city = mCityList.get(position);
                    if (city == null) {
                        setActivityResult(allAddrItemLists.get(0));
                        return;
                    }
                    allAddrItemLists.set(1, city); // 保存.
                    mDistrictList = mDataBaseHelper.queryDistrict(city.getId());
                    if (mDistrictList.size() > 0) {
                        mDistrictList.add(0, null);
                        leftTv.setText("" + city.getName());
                        mAddrDistrictAdapter.setDataUpdate(mDistrictList);
                        setJmpInThreeView();
                    } else {
                        setActivityResult(city);
                    }
                }
            }
        });
        // District
        mAddrDistrictAdapter = new AddrAdapter(mDistrictList);
        districtListview.setAdapter(mAddrDistrictAdapter);
        districtListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (jmpState == JMP_THREE_NUM) {
                    AddrItem district = mDistrictList.get(position);
                    if (district == null) {
                        setActivityResult(allAddrItemLists.get(1));
                        return;
                    }
                    allAddrItemLists.set(2, district); // 保存.
                    setActivityResult(district);
                }
            }
        });
    }

    public void setActivityResult(AddrItem addrItem) {
        Intent intent = new Intent();
        // 比如 四川省.
        intent.putExtra("addr1", allAddrItemLists.get(0).getName());
        intent.putExtra("id1", allAddrItemLists.get(0).getId());
        //  比如 成都市.
        intent.putExtra("addr2", allAddrItemLists.get(1).getName());
        intent.putExtra("id2", allAddrItemLists.get(1).getId());
        // 比如 武侯区.
        intent.putExtra("addr3", allAddrItemLists.get(2).getName());
        intent.putExtra("id3", allAddrItemLists.get(2).getId());
        // 已经选择的地区.
        String selectAddr = addrItem.getName();
        intent.putExtra("selectAddr", selectAddr);
        Log.w(TAG + " selectAddr:" + selectAddr);
        // 返回值.
        setResult(RESULT_ADDR_STATE, intent);
        finish();
    }

    /**
     * 返回.
     */
    @OnClick(R.id.left_lay)
    public void leftLayOnClick(View view) {
        setJmpOutView();
    }

    private void setJmpOutView() {
        animationOut = AnimationUtils.loadAnimation(mContext, R.anim.addr_view_out);
        switch (jmpState) {
            case JMP_FIRST_NUM:
                super.onBackPressed();
                break;
            case JMP_SECOND_NUM:
                cityListview.setVisibility(View.GONE);
                cityListview.setAnimation(animationOut);
                backIv.setImageResource(R.drawable.addr_left_close);
                jmpState = JMP_FIRST_NUM;
                leftTv.setText("" + "添加城市信息");
                break;
            case JMP_THREE_NUM:
                districtListview.setVisibility(View.GONE);
                districtListview.setAnimation(animationOut);
                jmpState = JMP_SECOND_NUM;
                leftTv.setText("" + saveProvince);
                saveProvince = "";
                break;
        }
    }

    // test
    @OnClick(R.id.right_lay)
    void rightLayOnClick(View view) {
        setJmpInView();
    }

    // test
    private void setJmpInView() {
        switch (jmpState) {
            case JMP_FIRST_NUM:
                setJmpInSecondView();
                break;
            case JMP_SECOND_NUM:
                setJmpInThreeView();
                break;
        }
    }

    private void setJmpInSecondView() {
        animationIn = AnimationUtils.loadAnimation(mContext, R.anim.addr_view_in);
        cityListview.setVisibility(View.VISIBLE);
        cityListview.setAnimation(animationIn);
        backIv.setImageResource(R.drawable.addr_left_back);
        jmpState = JMP_SECOND_NUM;
    }

    private void setJmpInThreeView() {
        animationIn = AnimationUtils.loadAnimation(mContext, R.anim.addr_view_in);
        districtListview.setVisibility(View.VISIBLE);
        districtListview.setAnimation(animationIn);
        jmpState = JMP_THREE_NUM;
    }

    @Override
    public void onBackPressed() {
        setJmpOutView();
    }

    public class AddrAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        private List<AddrItem> mAddrList;

        public AddrAdapter(List<AddrItem> addrList) {
            mInflater = LayoutInflater.from(mContext);
            mAddrList = addrList;
        }

        public void setDataUpdate(List<AddrItem> addrList) {
            mAddrList = addrList;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mAddrList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final MyViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.listitem_addr, parent,
                        false);
                holder = new MyViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (MyViewHolder) convertView.getTag();
            }
            //
            AddrItem addrItem = mAddrList.get(position);
            if (addrItem != null) {
                holder.name_tv.setText("" + addrItem.getName());
            } else {
                holder.name_tv.setText("全部");
            }
            return convertView;
        }

        private class MyViewHolder {
            public TextView name_tv;

            public MyViewHolder(View v) {
                name_tv = (TextView) v.findViewById(R.id.name_tv);
            }
        }

    }

}
